<template>
  <Dialog title="收款" :loading="loading" :visible="visible" button-text="提交" :reveal="true" class="two-columns" @close="handleClose" @confirm="balanceSubmit">
    <!-- 已收款详情 -->
    <el-row flex>
      <el-col :span="12">{{ currentRow.customerName }}：（{{ currentRow.customerPhone | phoneHandle }}）</el-col>
      <el-col :span="12"><span class="label-width">会员号：</span>{{ currentRow.customerCode }}</el-col>
      <el-col :span="12" class="margin-top"><span class="label-width">业务单类型：</span>{{ currentRow.paymentType }}</el-col>
      <el-col :span="12" class="margin-top"><span class="label-width">业务单号：</span>{{ currentRow.orderCode }}</el-col>
      <el-col :span="12" class="margin-top"><span class="label-width">开单人：</span>{{ currentRow.drawer }}</el-col>
      <el-col :span="12" class="margin-top"><span class="label-width">开单时间：</span>{{ currentRow.createdTime }}</el-col>
      <div class="margin-top clear">
        <ul v-if="currentRow.orderList && currentRow.orderList.length>0">
          <li class="detailed">
            商品清单：
          </li>
          <li v-for="(items,i) of currentRow.orderList" :key="i">
            <el-row v-if="currentRow.paymentType !== '充值单'" style="width:100%">
              <el-row type="flex">
                <el-col :span="20" class="product">
                  <el-button class="bg" type="text">{{ items.productAttrib.split("类")[0] }}</el-button><span style="margin-left:10px;line-height:30px">{{ items.name || items.projectName || items.dataName }}</span>
                  <p style="margin:0px 10px;">{{ items.printName }}</p>
                </el-col>
                <el-col :span="2" :offset="2">
                  x {{ items.contentCount }}
                </el-col>
              </el-row>
              <el-row v-if="items.productAttrib !== '套餐类'" class="grey" type="flex">
                <el-col :span="20">
                  <span>规格：{{ items.specl }}</span>
                  <span>单位：{{ items.unit }}</span>
                  <span v-if="items.type === '项目'">使用次数：{{ items.usageCount }}</span>
                </el-col>
              </el-row>
              <el-row type="flex" class="price">
                <el-col :span="6">
                  售价：<span class="text-color">￥{{ items.price }}</span>
                </el-col>
                <el-col :span="5" :offset="11">
                  折扣率：{{ items.discountRate }}%
                </el-col>
                <el-col :span="5">
                  折后价：￥{{ items.discountPrice }}
                </el-col>
              </el-row>
            </el-row>
            <el-row v-if="currentRow.paymentType === '充值单'" style="width:100%">
              <el-row type="flex">
                <el-col :span="20" class="product">
                  <el-button class="bg" type="text">{{ items.productAttrib.split("类")[0] }}</el-button><span style="margin-left:10px;line-height:30px">{{ items.contentType }}</span>
                  <p style="margin:0px 10px;">{{ items.printName }}</p>
                </el-col>
                <el-col :span="2" :offset="2">
                  x 1
                </el-col>
              </el-row>
              <el-row type="flex" class="price">
                <el-col :span="6">
                  售价：<span class="text-color">￥{{ items.price }}</span>
                </el-col>
              </el-row>
            </el-row>
          </li>
        </ul>
      </div>
      <el-col class="margin-top">收款类型<span class="pull-right">{{ currentRow.payType }}</span></el-col>
      <el-col class="margin-top">收款金额<span class="pull-right">￥{{ currentRow.orderAmount }}</span></el-col>
      <el-col v-if="currentRow.paymentType !== '充值单'" class="margin-top">使用余额(可使用￥{{ currentRow.capital }})
        <span v-if="!isBalance" class="pull-right" @click="isBalance = true">-￥{{ balance }}<i class="el-icon-arrow-right" /></span>
        <div v-if="isBalance" class="pull-right buttons">
          <el-input v-model="balance" class="el-select-y" @input="balanceChange()" />
          <el-button @click="balanceDetermine">确定</el-button>
        </div>
      </el-col>
      <el-col class="margin-top"><span class="pull-right">应收款：<span class="font-color">￥{{ receivables }}</span></span></el-col>
      <el-col class="margin-top"><span class="pull-right buttons"><el-button class="button-color" @click="addMode"><i class="el-icon-plus" />支付方式</el-button></span></el-col>
      <el-col class="margin-top">收款</el-col>
      <el-table :data="tableData" border>
        <el-table-column prop="date" label="支付方式">
          <template slot-scope="{ row }">
            <el-select v-model="row.mode" class="el-select-y" placeholder="请选择支付方式">
              <el-option
                v-for="item in paymentList"
                :key="item.id"
                :label="item.value"
                :value="item.value"
              />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="金额（元）">
          <template slot-scope="{ row }">
            <el-input v-model="row.money" class="el-select-y" @input="moneyChange" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button type="text" @click="deleteMode(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col class="margin-top"><span class="pull-right">实收款：<span class="font-color">￥{{ paidIn }}</span></span></el-col>
      <el-col class="margin-top"><el-input v-model="remark" class="box-yy" type="textarea" placeholder="请输入备注" /></el-col>
    </el-row>
  </Dialog>
</template>
<script>
import Dialog from "@/components/Dialog";
export default {
  filters: {
    phoneHandle(val) {
      if (!val) return '';
      let phone = "";
      phone = val.substring(0, 3) + "****" + val.substring(val.length - 4, val.length);
      return phone;
    },
  },
  components: {
    Dialog,
  },
  props: {
    currentRow: {
      type: Object,
      default: () => ({}),
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      datas: {},
      tableData: [{ id: 0, mode: "", money: 0 }],
      receivables: 0, // 应收款
      paidIn: 0, // 实收款
      paymentList: [{ id: 0, value: "支付宝" }, { id: 1, value: "微信" }, { id: 2, value: "现金" }, { id: 3, value: "银联" }, { id: 4, value: "POS" }, { id: 5, value: "其他" }],
      balance: 0,
      isBalance: false,
      remark: "",
    };
  },
  watch: {
    currentRow: {
      handler(val) {
        this.loading = false;
        this.tableData = [];
        const json = {
          money: val.orderAmount,
          mode: "",
        };
        this.balance = 0;
        this.tableData.push(json);
        this.paidIn = val.orderAmount;
        this.receivables = val.orderAmount;
      },
      deep: true,
    },
  },
  methods: {
    handleClose() {
      this.$emit('close');
    },
    addMode() {
      const json = { id: this.tableData.length, mode: "", money: 0 };
      this.tableData.push(json);
    },
    deleteMode(row) {
      this.tableData.splice(row.id, 1);
    },
    balanceChange() {
      this.balance = parseInt(this.balance);
      if (!this.balance) {
        this.balance = 0;
      }
      if (this.balance < 0) {
        this.balance = 0;
      }
      if (this.balance > parseInt(this.currentRow.orderAmount)) {
        this.balance = parseInt(this.currentRow.orderAmount);
      }
      if (this.balance > parseInt(this.currentRow.capital)) {
        this.balance = parseInt(this.currentRow.capital);
      }
    },
    balanceDetermine() {
      this.isBalance = false;
      this.receivables = this.currentRow.orderAmount - this.balance;
      this.tableData[0].money = this.receivables;
      this.paidIn = this.receivables;
      if (this.receivables === 0) {
        this.tableData = [];
      }
    },
    async balanceSubmit() {
      this.loading = true;
      if (this.paidIn !== this.receivables) {
        this.$message.error("应收款=实收款，才可以提交");
        this.loading = false;
        return false;
      }
      const list = [];
      if (this.balance > 0) {
        const json = {
          payAmount: this.balance,
          logType: "余额流水",
          payType: "余额支付",
          direction: "支出",
          type: "消费",
        };
        list.push(json);
      }
      if (this.tableData.length > 0) {
        for (let i = 0; i < this.tableData.length; i++) {
          const item = this.tableData[i];
          if (!item.mode) {
            this.loading = false;
            this.$message.error("请选择支付方式");
            return;
          }
          if (item.money > 0) {
            const json = {
              payAmount: item.money,
              logType: "实收流水",
              payType: item.mode,
              direction: "收款",
            };
            list.push(json);
          }
        }
      }
      const res = await this.$api['financialCenter/checkstand/addPayment']({
        id: this.currentRow.id,
        actualAmount: this.paidIn,
        remark: this.remark,
        logList: list,
        residualAmount: this.balance.toString(),
      });
      this.loading = false;
      if (res.code !== 200) {
        this.$message.error(res.msg);
        return false;
      }
      this.$emit("success");
    },
    moneyChange() {
      let num = 0;
      this.tableData.forEach(item => {
        num += Number(item.money);
      });
      this.paidIn = num;
    },
  },
};
</script>
<style lang="scss" scoped>
.pull-right{
  float: right;
}
.font-color{
  color:#FF6600;
}
.button-color{
  color:#0099FF;
  border-color:#0099FF;
}
.clear{
  clear: both;
}
.detailed{
    margin: 15px 0;
  }
  .el-col-offset-8,.el-col-offset-2 {
  .el-button {
    margin-top: 15px;
    width: 100%;
    height: 40px;
  }
}
/deep/.el-form-item__content{
  margin-left: 50px !important;
}
  .span{
    color: #ff6600;
  }
  .grey{
    color: #aaaaaa;
  }
  .bg{
    background: rgba(211,223,247,0.7);
    width: 50px;
    height: 30px;
    text-align: center;
    border-radius: 10px;
    color: #666;
  }
  .product{
    display: flex;
  }
  .price{
    border-bottom: 1px solid #797979;
    padding-bottom: 10px;
    .el-col{
      display: flex;
    }
  }
  .el-col,.el-col-12{
    line-height: 40px;
  }
  li{
    display: flex;
    margin-top: 5px;
  }
  .detailed{
    margin: 15px 0;
  }
  div>span{
    margin-right: 25px;
  }
  .box-yy /deep/.el-textarea__inner{
  border: 1px solid #9baed3;
  }
</style>
